﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace test
{
    public partial class FormEditSort : Form
    {
        DataSet ds = new DataSet();
        public int id = 0;
        public FormEditSort()
        {
            InitializeComponent();
        }
        //从数据库得到房屋类别
        private void GetSort()
        {
            DBHelper dBHelper = new DBHelper();
            try
            {
                String sql = "select * from RoomSize order by id";
                MySqlDataAdapter adapter = new MySqlDataAdapter(sql, dBHelper.Connection);
                //填充数据
                adapter.Fill(ds, "Sort");
                //绑定数据
                this.cboSort.DataSource = ds.Tables["Sort"];
                //隐藏的列
                this.cboSort.ValueMember = "RoomSize";
                //显示的列
                this.cboSort.DisplayMember = "RoomSize";
            }catch(Exception e)
            {
                MessageBox.Show("获取列表失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        //窗体加载事件
        private void FormEditSort_Load(object sender, EventArgs e)
        {
            if(id==0)//添加操作
            {
                this.GetSort();
            }
            else
            {
                this.GetRoomSort();
                this.btn_save.Text = "修改";
            }
        }
        //新增的保存按钮
        private void btn_save_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                if (id == 0)//判断是否为新增
                {
                    Insert();
                }
                else//否则为修改
                {
                    UpdateHotel();
                }
            }
        }
        //非空验证
        private bool CheckInput()
        {
            bool ok = true;
            if (text_Name.Text.Trim().Length == 0)
            {
                MessageBox.Show("名称不能为空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ok = false;
            }
            return ok;
        }
        //新增
        private void Insert()
        {
            DBHelper dBHelper = new DBHelper();
            try
            {
                String sql = String.Format("insert into hotel(name,roomsize,price,leased,surplus,is_full) values('{0}','{1}',{2},{3},{4},'{5}')", text_Name.Text.Trim(),cboSort.SelectedValue, this.numPrice.Value, this.leased.Value, this.surplus.Value,this.checkLeased.Checked?"是":"否");
                MySqlCommand cmd = new MySqlCommand(sql, dBHelper.Connection);
                dBHelper.OpenConnection();
                int result = cmd.ExecuteNonQuery();
                if (result == 1)
                {
                    MessageBox.Show("添加成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }

            }catch(Exception e)
            {
                MessageBox.Show("插入失败！"+e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                dBHelper.CloseConnection();
            }
        }
        private void GetRoomSort()
        {
            //获取类别值
            GetSort();
            DBHelper dBHelper = new DBHelper();
            try
            {
                //创建sql语句
                String sql = String.Format("select * from hotel where id = {0}", id);
                //执行工具
                MySqlCommand cmd = new MySqlCommand(sql, dBHelper.Connection);
                //打开连接
                dBHelper.OpenConnection();
                //执行，读操作，返回sqldata对象
                MySqlDataReader reader = cmd.ExecuteReader();
                //判断是否存在
                if (reader.Read())
                {
                    //数据集赋值到编辑窗口
                    this.text_Name.Text = reader["name"].ToString();//reader读取Object类型，tostring用于转换
                    this.cboSort.SelectedValue = reader["roomsize"].ToString();
                    this.numPrice.Value = Convert.ToDecimal(reader["price"]);
                    //checkbox是否选中
                    if (reader["is_full"].ToString() == "是")
                    {
                        this.checkLeased.Checked =true;
                    }
                    this.surplus.Value = Convert.ToDecimal(reader["surplus"]);
                    this.leased.Value = Convert.ToDecimal(reader["leased"]);
                }
                reader.Close();
            }catch(Exception e)
            {
                MessageBox.Show("获取类别值出错！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                dBHelper.CloseConnection();
            }
        }
        //修改按钮的更新操作
        private void UpdateHotel()
        {
            DBHelper dBHelper = new DBHelper();
            try
            {
                //创建sql语句
                String sql = String.Format("update hotel set name = '{0}', roomsize='{1}',price={2},leased={3},surplus={4},is_full='{5}' where id = {6}", this.text_Name.Text.Trim(), this.cboSort.SelectedValue, this.numPrice.Value, this.leased.Value, this.surplus.Value, this.checkLeased.Checked ? "是" : "否", this.id);
                //执行工具
                MySqlCommand cmd = new MySqlCommand(sql, dBHelper.Connection);
                //打开数据库连接
                dBHelper.OpenConnection();
                //执行
                int result = cmd.ExecuteNonQuery();
                //判断
                if (result==1)//如果受影响行数等于1
                {
                    MessageBox.Show("修改成功！");
                    this.Close();
                }
            }catch(Exception e)
            {
                MessageBox.Show("修改失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                dBHelper.CloseConnection();
            }
        }
        //checkbox选择更改时，租出窗口不可更改
        private void checkLeased_CheckedChanged(object sender, EventArgs e)
        {
            if (checkLeased.Checked==true)
            {
                this.surplus.Enabled = false;
                this.surplus.Value = 0;
            }
            else
            {
                this.surplus.Enabled = true;
            }
        }

        private void btn_cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
